// pages/my/cash/affirmRecharge.js
const App = getApp()
const api = require('../../../utils/api.js');
const util = require('../../../utils/util.js');


//支付方式

Page({
  data: {
    selectPayId: null,
    money: 0.00,

    items: [
      /**
            {
              name: 'alipay', value: '支付宝支付',
              memo: '推荐有支付宝的用户使用',
              img: '../../../image/my/u2172.png',
              isSelect: false,
            },
       */

      {
        name: 'wxpay', value: '微信支付',
        memo: '推荐安装微信5.0及以上版本的用户使用',
        img: '../../../image/my/u2174.png',
        isSelect: false,
        //checked: 'true'
      }
    ],

    form: {
      name: '',
      is_def: 0,
    }
  },

  preventTouchMove: function () {
  },
  onLoad: function (options) {

    this.setData({
      money: options.money
    })

  },


  submitForm(e) {
    const params = e.detail.value
    const payType = this.data.selectPayId
    console.log("payTpe>>>>>>>>" + payType)
    console.log("money>>>>>>>>" + this.data.money)
    console.log("selectPayId>>>>>>>>" + this.data.selectPayId)

    if (payType == 'wxpay') {
      var that = this

      wx.login({
        success: function (res) {

          console.log("----------code is --------");
          console.log(res);
          console.log(res.code);

          if (res.code) {


                console.log("调服务器后端接口>>>>>>>>")
                console.log("that.data.money>>>>>>>>" + that.data.money)

                //#余额充值确认支付

                let bodyData = {
                  detail: '余额充值',

                  payment_way_key: 'ZFFS_02',// 微信小程序 支付方式 ZFFS_02
                  business_type_key: "JYLX_01",//类型 
                  order_type: 'DDLX_18',   
                  amount: that.data.money,
                  //out_trade_no: '2017111018060304216463',
                  data_source: 'SJLY_17',// 微信小程序 数据来源 SJLY_17

                  member_id: App.globalData.memberInfo.member_id,
                  //openid: res.data.openid
                  js_code:res.code
                }

                let paramsData = {
                  detail: '余额充值',

                  payment_way_key: 'ZFFS_02',// 微信小程序 支付方式 ZFFS_02
                  business_type_key: "JYLX_01",//类型 
                  order_type: 'DDLX_18',  
                  amount: that.data.money,
                  //out_trade_no: '2017111018060304216463',
                  data_source: 'SJLY_17',// 微信小程序 数据来源 SJLY_17

                  out_trade_body: JSON.stringify(bodyData),
                  member_id: App.globalData.memberInfo.member_id,
                  //openid: res.data.openid
                  js_code: res.code
                }

                const data = {
                  service: 'finance.balanceRecharge',
                  params: JSON.stringify(paramsData),
                  url_type: 'finance',
                  app_token: App.globalData.app_token,         
                  security_code: App.globalData.security_code
                }


                console.log("data...........")
                console.log(data)

                api.reqData({
                  data,
                  success: (res) => {
                    var resData = res.data;

                    console.log("结果是...........")
                    console.log(resData)

                    var msgContent = '';
                    if (res.data.rsp_code == 'fail') {
                      var error_code = res.data.error_code
                      var error_msg = res.data.error_msg


                      if (error_code == 'wechat_error' || error_code == 'data_source_error') {
                        wx.showModal({
                          title: '支付结果',
                          content: '支付失败',
                          showCancel: false,
                        })
                      }

                      if (error_code == 'member_no_exist') {
                        wx.showModal({
                          title: '支付结果',
                          content: error_msg,
                          showCancel: false,
                        })
                      }

                    } else {


                      //var appid = resData.data.appId
                      var noncestr = resData.data.nonceStr
                      var packagestr = resData.data.package
                      var paysign = resData.data.paySign
                      var timestamp = resData.data.timeStamp
                      //var transaction_no = resData.data.transaction_no

                      /////////////////////////////////////////////////////////////////////////

                      wx.requestPayment({
                        'timeStamp': timestamp,
                        'nonceStr': noncestr,
                        'package': packagestr,
                        'signType': 'MD5',
                        'paySign': paysign,
                        'success': function (res) {
                          console.log(res+"支付成功++++++++++++++++++++++++++++++++")
                    
                          wx.showModal({
                            title: '支付结果',
                            image:'../../../image/my/u2186.png',
                            content: '支付成功',
                            confirmColor:"#f3630a",
                            showCancel: false,
                            success: function (res) {
                              if (res.confirm) {
                                console.log(msgContent)
                                wx.navigateBack({
                                  delta: 2
                                })
                              }
                            }
                          })

                        },
                        'fail': function (res) {
                          console.log("--------------fail ------------------")
                          console.log(res)

                          if (res.errMsg == 'requestPayment:fail cancel') {

                            wx.showModal({
                              title: '支付结果',
                              content: '用户取消',
                              showCancel: false,
                            })
                          }


                          if (error_code == 'wechat_error' || error_code == 'data_source_error') {
                            wx.showModal({
                              title: '支付结果',
                              content: '支付失败',
                              showCancel: false,
                            })
                          }

                          if (error_code == 'member_no_exist') {
                            wx.showModal({
                              title: '支付结果',
                              content: error_msg,
                              showCancel: false,
                            })
                          }

                        },
                        'complete': function (res) {

                          console.log("--------------complete ------------------")
                          console.log(res.errMsg)


                        }
                      })

                      ////////////////////////////////////////////////////////////////////////

                    }


                  },
                  fail: (res) => {
                    console.log("fail 返回 res.data>>>" + res.data);
                  },
                  complete: (res) => {
                    console.log(res.data);
                    console.log(res.data.error_msg);
                    if (res.data.error_msg =="交易失败,每天只能充值两次"){
                      wx.showModal({
                        title: '支付结果',
                        confirmColor: '#F3630A',
                        content: '交易失败,每天只能充值两次',
                        showCancel: false,
                      })
                    }
                  }
                })


          }
        }
      });




    } else if (payType == null) {
      App.WxService.showModal({
        title: '友情提示',
        content: '请选择支付方式',
        confirmColor: "#f3630a",
        showCancel: !1,
      })
      return false
    }
  },

  selectPayType: function (e) {
    var selectPayId = e.target.dataset.payid;
    console.log("selectPayId>>>>>>>>>>>" + selectPayId);


    for (var i = 0; i < this.data.items.length; i++) {
      if (this.data.items[i].name == selectPayId) {
        this.data.items[i].isSelect = true
      } else {
        this.data.items[i].isSelect = false
      }
    }

    this.setData({
      selectPayId: selectPayId,
      items: this.data.items,
    })
  }

})